﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Texfina.Core.Common;  
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;  
using Texfina.Entity.Rh;
using RHBOQry = Texfina.BOQry.Rh;
using RHBORpt = Texfina.BORpt.Rh;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;

namespace RH
{
    public partial class wfIngresoSubsidioBnd : SearchPage 
   {
       public const int _GRDLIST_STANULADO = 7;
    
      protected void Page_Load(object sender, EventArgs e)
      {
                       
         this.LoadInitialSettings();
         if(this.IsPostBack){
            this.LoadInPostBack();
            return;
         } 

         this.LoadData();
      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label) this.Master.FindControl("lblTitle");
         lbl.Text = "Subsidio";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void SearchData()
      {

          ESubsidioBandeja objE = new ESubsidioBandeja();

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.txtidSubsidio.Text == "")
            objE.IdSubsidio = null;
         else
            objE.IdSubsidio = this.txtidSubsidio.Text;

         if (this.txtidPersonal.Text == "")
             objE.IdPersonal = null;
         else
             objE.IdPersonal = this.txtidPersonal.Text;
          
         if (this.txtnuDocIden.Text == "")
            objE.NuDocIden  = null;
         else
            objE.NuDocIden = this.txtnuDocIden.Text;

         if (this.txtdsPersonal.Text == "")
            objE.DsApellNom = null;
         else
            objE.DsApellNom = this.txtdsPersonal.Text;

         if (this.txtfeSubsidio.Text == "")
            objE.FeSubsidio = null;
         else
            objE.FeSubsidio = DateTime.Parse(this.txtfeSubsidio.Text);

         
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Subsidio));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
          String strQS= "";

         switch(e.Item.Value){
            case ConstMenuToolbarValue.BUTTON_NEW:
                 strQS = "wfIngresoSubsidioMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdPersonal=&IdSubsidio=";
               break; 
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      { 
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA= FormEditAction.AddNew;
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);
         switch(e.CommandName)
         {
             case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
             case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
             case ConstGridRowCommand.GRIDPREVIEW:
               PrintReport(ConstGridRowCommand.GRIDPREVIEW);
               break;
         }
         String strQS = "wfIngresoSubsidioMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdPersonal=" + dr["idPersonal"].ToString() + "&IdSubsidio=" + dr["idSubsidio"].ToString();
         Response.Redirect(strQS);
      
      }

      protected void grdBttCmdPrevPostBackDataBinding(Object sender, EventArgs e)
      {
          ImageButton ctl = (ImageButton)sender;
          ScriptManager sm = ScriptManager.GetCurrent(this.Page);
          sm.RegisterPostBackControl(ctl);
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstEstadoRegPersonal.PENDIENTE)
              {e.Row.ForeColor = System.Drawing.Color.Green;}
          }

      }
      protected void bttOk_Click(object sender, EventArgs e)
      {
          this.SearchData();
      }

      public override void PrintReport(string key)
      {
          
          DataSet ds = (DataSet)this.GetDataReport(key);
          if (ds.Tables[0].Rows.Count > 0)
          {
              ReportDocument objRpt = new ReportDocument();
              objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/CuentaCorriente.rpt"));
              objRpt.SetDataSource(ds.Tables[0]);

              MemoryStream ms = new MemoryStream();

              Response.Clear();

              ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
              objRpt.Dispose();
              objRpt = null;

              Response.AddHeader("Content-Disposition", "attachment;filename=PRESTAMO.pdf");

              Response.Buffer = true;
              Response.ContentType = "application/pdf";

              ms.WriteTo(Response.OutputStream);
              Response.Flush();
              Response.End();
          }

      }
      public override object GetDataReport(string key)
      {
          EPrestamo objE = new EPrestamo();
          RHBORpt.IPersonal objBODet = (RHBORpt.IPersonal)WCFHelper.GetObject<RHBORpt.IPersonal>(typeof(RHBORpt.Personal));

          DataRow dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
          objE.IdEmpresa =  dr["idEmpresa"].ToString();
          objE.IdPersonal = dr["idPersonal"].ToString();
          objE.IdPrestamo = dr["idPrestamo"].ToString();

          DataTable dt = objBODet.GetByCriteriaPrestamo(objE);
         

          DataSet ds = new DataSet();
          ds.Tables.Add(dt);
        

          return ds;

      }
   }
}